﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;

namespace ConsoleBuildBill
{
	class Program
	{
		static void Main(string[] args)
		{
			try
			{
				DO.Common.Trace.SetLogFile(DO.Common.DreamOnConfig.logFolder, "BuildBill");
				string bill_folder = DO.Common.DreamOnConfig.billFolder;
				string url = DO.Common.DreamOnConfig.url_webBill + "Bill.aspx";

				using (DO.Common.DataFunction df = new DO.Common.DataFunction(DO.Common.DreamOnConfig.connectionString_web))
				{
					df.RunSqlCommand("[dbo].[UpdateASSET];");

					string SQL = "select * from CUSTOMERS where [ID] <> 'A111111111'";
					string html;
					DataSet ds = new DataSet();
					df.RunSqlCommand(SQL, out ds);


					for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
					{
						string id = ds.Tables[0].Rows[i]["ID"].ToString();
						//取得客戶帳單
						DO.Common.Trace.InfoWrite("BuildBill", "url=" + url + "?" + "id=" + id);
						html = DO.Common.WebBrowser.getHTML(url, "id=" + id, "GET", null, "UTF-8");
						//html.Replace("<!-replace_ps->", replace_ps);
						

						//儲存
						string filename = DateTime.Now.Year + "_" + DateTime.Now.Month + "月份對帳單_" + id;
						using (System.IO.StreamWriter write = new System.IO.StreamWriter(bill_folder + @"\" + filename + ".html", false, System.Text.Encoding.GetEncoding("UTF-8")))
						{
							write.Write(html);
							write.Flush();
						}
						DO.Common.Trace.InfoWrite("BuildBill", "done " + bill_folder + @"\" + filename);
					}

				}
			}
			catch (Exception ex)
			{
				DO.Common.Trace.ErrorWrite("BuildBill", ex.ToString());
			}

		}
	}
}
